#include "IconFactory.h"
#include <QPainter>
#include <QPixmap>
#include <QPen>
#include <QPainterPath>

// --- 私有辅助函数声明 (放在cpp文件顶部) ---
// 为了代码整洁，我们将每个图标的绘制逻辑封装在独立的函数中
namespace IconPainters {
    void drawSingleColumn(QPainter& painter, const QRect& rect, const QColor& color);
    void drawTwoColumns(QPainter& painter, const QRect& rect, const QColor& color);
    void drawThreeColumns(QPainter& painter, const QRect& rect, const QColor& color);
    void drawZoomIn(QPainter& painter, const QRect& rect, const QColor& color);
    void drawZoomOut(QPainter& painter, const QRect& rect, const QColor& color);
    void drawImport(QPainter& painter, const QRect& rect, const QColor& color);
    void drawExport(QPainter& painter, const QRect& rect, const QColor& color);
    void drawFitHeight(QPainter& painter, const QRect& rect, const QColor& color);
    void drawFitWidth(QPainter& painter, const QRect& rect, const QColor& color);
    void drawSystemLogin(QPainter& painter, const QRect& rect, const QColor& color);
    void drawSettings(QPainter& painter, const QRect& rect, const QColor& color);
}

// --- IconFactory 核心实现 ---
QIcon IconFactory::createIcon(IconFactory::IconType type, const QSize& size)
{
    // 1. 创建一个带Alpha通道的Pixmap作为画布
    QPixmap pixmap(size);
    pixmap.fill(Qt::transparent); // 设置为透明背景

    // 2. 创建QPainter在Pixmap上进行绘制
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing, true); // 开启抗锯齿，使线条平滑

    // 3. 定义样式参数
    const QColor iconColor("#6c6c6c"); // 高级灰
    const int padding = 4; // 内边距
    const QRect contentRect = pixmap.rect().adjusted(padding, padding, -padding, -padding);
    
    // 4. 根据类型选择对应的绘制函数
    switch (type) {
        case SingleColumn:
            IconPainters::drawSingleColumn(painter, contentRect, iconColor);
            break;
        case TwoColumns:
            IconPainters::drawTwoColumns(painter, contentRect, iconColor);
            break;
        case ThreeColumns:
            IconPainters::drawThreeColumns(painter, contentRect, iconColor);
            break;
        case ZoomIn:
            IconPainters::drawZoomIn(painter, contentRect, iconColor);
            break;
        case ZoomOut:
            IconPainters::drawZoomOut(painter, contentRect, iconColor);
            break;
        case Import:
            IconPainters::drawImport(painter, contentRect, iconColor);
            break;
        case Export:
            IconPainters::drawExport(painter, contentRect, iconColor);
            break;
        case FitHeight:
            IconPainters::drawFitHeight(painter, contentRect, iconColor);
            break;
        case FitWidth:
            IconPainters::drawFitWidth(painter, contentRect, iconColor);
            break;
        case SystemLogin:
            IconPainters::drawSystemLogin(painter, contentRect, iconColor);
            break;
        case Settings:
            IconPainters::drawSettings(painter, contentRect, iconColor);
            break;
    }

    painter.end();

    // 5. 从绘制好的Pixmap创建QIcon并返回
    return QIcon(pixmap);
}

// --- 私有辅助函数实现 ---
namespace IconPainters {

    void drawSingleColumn(QPainter& painter, const QRect& rect, const QColor& color) {
        // 绘制A4纸外框轮廓（与其他图标外框尺寸一致）
        painter.setPen(QPen(color, 2));
        painter.setBrush(Qt::NoBrush);
        painter.drawRoundedRect(rect, 2, 2);

        // 绘制内部单张图片区域
        QRect contentRect = rect.adjusted(4, 4, -4, -4);
        painter.setBrush(color);
        painter.setPen(Qt::NoPen);
        painter.drawRect(contentRect);
    }

    void drawTwoColumns(QPainter& painter, const QRect& rect, const QColor& color) {
        // 绘制A4纸外框轮廓（与其他图标外框尺寸一致）
        painter.setPen(QPen(color, 2));
        painter.setBrush(Qt::NoBrush);
        painter.drawRoundedRect(rect, 2, 2);

        // 绘制内部2x2布局（列优先排列）
        QRect contentRect = rect.adjusted(4, 4, -4, -4);
        int colSpacing = 3;
        int rowSpacing = 1;
        int colWidth = (contentRect.width() - colSpacing) / 2;
        int rowHeight = (contentRect.height() + rowSpacing) / 2; // 稍微增加高度，充分利用空间

        painter.setBrush(color);
        painter.setPen(Qt::NoPen);

        // 列优先布局：左列（位置1,2），右列（位置3,4）
        painter.drawRect(contentRect.x(), contentRect.y(), colWidth, rowHeight);                     // 位置1
        painter.drawRect(contentRect.x(), contentRect.y() + rowHeight + rowSpacing, colWidth, rowHeight); // 位置2
        painter.drawRect(contentRect.x() + colWidth + colSpacing, contentRect.y(), colWidth, rowHeight);     // 位置3
        painter.drawRect(contentRect.x() + colWidth + colSpacing, contentRect.y() + rowHeight + rowSpacing, colWidth, rowHeight); // 位置4
    }
    
    void drawThreeColumns(QPainter& painter, const QRect& rect, const QColor& color) {
        // 绘制A4纸外框轮廓（与其他图标外框尺寸一致）
        painter.setPen(QPen(color, 2));
        painter.setBrush(Qt::NoBrush);
        painter.drawRoundedRect(rect, 2, 2);

        // 绘制内部3x3布局（列优先排列）
        QRect contentRect = rect.adjusted(4, 4, -4, -4);
        int colSpacing = 2;
        int rowSpacing = 1;
        int colWidth = (contentRect.width() - 2 * colSpacing) / 3;
        int rowHeight = (contentRect.height() + rowSpacing) / 3; // 稍微增加高度，充分利用空间

        painter.setBrush(color);
        painter.setPen(Qt::NoPen);

        // 列优先布局：第一列（位置1,2,3），第二列（位置4,5,6），第三列（位置7,8,9）
        // 第一列
        painter.drawRect(contentRect.x(), contentRect.y(), colWidth, rowHeight); // 位置1
        painter.drawRect(contentRect.x(), contentRect.y() + rowHeight + rowSpacing, colWidth, rowHeight); // 位置2
        painter.drawRect(contentRect.x(), contentRect.y() + 2 * (rowHeight + rowSpacing), colWidth, rowHeight); // 位置3

        // 第二列
        painter.drawRect(contentRect.x() + colWidth + colSpacing, contentRect.y(), colWidth, rowHeight); // 位置4
        painter.drawRect(contentRect.x() + colWidth + colSpacing, contentRect.y() + rowHeight + rowSpacing, colWidth, rowHeight); // 位置5
        painter.drawRect(contentRect.x() + colWidth + colSpacing, contentRect.y() + 2 * (rowHeight + rowSpacing), colWidth, rowHeight); // 位置6

        // 第三列
        painter.drawRect(contentRect.x() + 2 * (colWidth + colSpacing), contentRect.y(), colWidth, rowHeight); // 位置7
        painter.drawRect(contentRect.x() + 2 * (colWidth + colSpacing), contentRect.y() + rowHeight + rowSpacing, colWidth, rowHeight); // 位置8
        painter.drawRect(contentRect.x() + 2 * (colWidth + colSpacing), contentRect.y() + 2 * (rowHeight + rowSpacing), colWidth, rowHeight); // 位置9
    }

    void drawArrow(QPainter& painter, const QPoint& start, int angle) {
        painter.save();
        painter.translate(start);
        painter.rotate(angle);
        QPointF points[3] = {
            QPointF(0, 0),
            QPointF(-8, -3),
            QPointF(-8, 3)
        };
        painter.drawPolygon(points, 3);
        painter.restore();
    }
    
    
    void drawImport(QPainter& painter, const QRect& rect, const QColor& color) {
        QPen pen(color, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
        painter.setPen(pen);
        painter.setBrush(Qt::NoBrush);

        // Arrow
        int cx = rect.center().x();
        painter.drawLine(cx, rect.top(), cx, rect.top() + 14); // Stem
        painter.drawLine(cx, rect.top() + 14, cx - 5, rect.top() + 9); // Left head
        painter.drawLine(cx, rect.top() + 14, cx + 5, rect.top() + 9); // Right head

        // Tray
        painter.drawLine(rect.left(), rect.bottom(), rect.right(), rect.bottom()); // Base
        painter.drawLine(rect.left(), rect.bottom(), rect.left(), rect.bottom() - 6); // Left wall
        painter.drawLine(rect.right(), rect.bottom(), rect.right(), rect.bottom() - 6); // Right wall
    }

    void drawExport(QPainter& painter, const QRect& rect, const QColor& color) {
        QPen pen(color, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
        painter.setPen(pen);
        painter.setBrush(Qt::NoBrush);
        
        // Document shape
        int docWidth = rect.width() * 0.7;
        int docHeight = rect.height() * 0.8;
        int docLeft = rect.left() + (rect.width() - docWidth) / 2;
        int docTop = rect.bottom() - docHeight;
        painter.drawRect(docLeft, docTop, docWidth, docHeight);

        // Arrow
        int cx = rect.center().x() + 2;
        painter.drawLine(cx, docTop, cx, rect.top()); // Stem
        painter.drawLine(cx, rect.top(), cx - 5, rect.top() + 5); // Left head
        painter.drawLine(cx, rect.top(), cx + 5, rect.top() + 5); // Right head
    }

    void drawFitHeight(QPainter& painter, const QRect& rect, const QColor& color) {
        QPen pen(color, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin); // 使用稍细的线条以容纳更多细节
        painter.setPen(pen);
        painter.setBrush(Qt::NoBrush);

        // 1. 绘制外部容器
        painter.drawRect(rect);

        // 2. 绘制上下箭头
        int cx = rect.center().x();
        painter.drawLine(cx, rect.top(), cx, rect.bottom()); // 垂直中心线
        // 向上箭头
        painter.drawLine(cx, rect.top(), cx - 4, rect.top() + 4);
        painter.drawLine(cx, rect.top(), cx + 4, rect.top() + 4);
        // 向下箭头
        painter.drawLine(cx, rect.bottom(), cx - 4, rect.bottom() - 4);
        painter.drawLine(cx, rect.bottom(), cx + 4, rect.bottom() - 4);
        
        // 3. 绘制内部内容矩形 (窄一些)
        QRect innerRect = rect.adjusted(rect.width() / 4, 0, -rect.width() / 4, 0);
        painter.setPen(QPen(color, 3)); // 内容矩形使用标准粗细
        painter.drawRect(innerRect);
    }

    void drawFitWidth(QPainter& painter, const QRect& rect, const QColor& color) {
        QPen pen(color, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
        painter.setPen(pen);
        painter.setBrush(Qt::NoBrush);

        // 1. 绘制外部容器
        painter.drawRect(rect);

        // 2. 绘制左右箭头
        int cy = rect.center().y();
        painter.drawLine(rect.left(), cy, rect.right(), cy); // 水平中心线
        // 向左箭头
        painter.drawLine(rect.left(), cy, rect.left() + 4, cy - 4);
        painter.drawLine(rect.left(), cy, rect.left() + 4, cy + 4);
        // 向右箭头
        painter.drawLine(rect.right(), cy, rect.right() - 4, cy - 4);
        painter.drawLine(rect.right(), cy, rect.right() - 4, cy + 4);

        // 3. 绘制内部内容矩形 (矮一些)
        QRect innerRect = rect.adjusted(0, rect.height() / 4, 0, -rect.height() / 4);
        painter.setPen(QPen(color, 3)); // 内容矩形使用标准粗细
        painter.drawRect(innerRect);
    }
    

    void drawZoomIn(QPainter& painter, const QRect& rect, const QColor& color) {
        QPen pen(color, 3, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin); // 统一使用3px线条
        painter.setPen(pen);
        painter.setBrush(Qt::NoBrush); // 确保没有填充

        const int offset = 2; // Offset from corner
        // Top-left corner lines
        painter.drawLine(rect.topLeft() + QPoint(offset, 8), rect.topLeft() + QPoint(offset, offset));
        painter.drawLine(rect.topLeft() + QPoint(8, offset), rect.topLeft() + QPoint(offset, offset));

        // Top-right corner lines
        painter.drawLine(rect.topRight() + QPoint(-offset, 8), rect.topRight() + QPoint(-offset, offset));
        painter.drawLine(rect.topRight() + QPoint(-8, offset), rect.topRight() + QPoint(-offset, offset));

        // Bottom-left corner lines
        painter.drawLine(rect.bottomLeft() + QPoint(offset, -8), rect.bottomLeft() + QPoint(offset, -offset));
        painter.drawLine(rect.bottomLeft() + QPoint(8, -offset), rect.bottomLeft() + QPoint(offset, -offset));

        // Bottom-right corner lines
        painter.drawLine(rect.bottomRight() + QPoint(-offset, -8), rect.bottomRight() + QPoint(-offset, -offset));
        painter.drawLine(rect.bottomRight() + QPoint(-8, -offset), rect.bottomRight() + QPoint(-offset, -offset));
        
        // --- 新增: 中心绘制一个 "+" 号 ---
        QPoint center = rect.center();
        int crossSize = 6; // +号的半边长度
        painter.drawLine(center.x() - crossSize, center.y(), center.x() + crossSize, center.y()); // 横线
        painter.drawLine(center.x(), center.y() - crossSize, center.x(), center.y() + crossSize); // 竖线
    }

    void drawZoomOut(QPainter& painter, const QRect& rect, const QColor& color) {
        QPen pen(color, 3, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin); // 统一使用3px线条
        painter.setPen(pen);
        painter.setBrush(Qt::NoBrush); // 确保没有填充

        const int offset = 2; // Offset from corner
        // Top-left corner lines
        painter.drawLine(rect.topLeft() + QPoint(offset, offset), rect.topLeft() + QPoint(offset, 8));
        painter.drawLine(rect.topLeft() + QPoint(offset, offset), rect.topLeft() + QPoint(8, offset));

        // Top-right corner lines
        painter.drawLine(rect.topRight() + QPoint(-offset, offset), rect.topRight() + QPoint(-offset, 8));
        painter.drawLine(rect.topRight() + QPoint(-offset, offset), rect.topRight() + QPoint(-8, offset));
        
        // Bottom-left corner lines
        painter.drawLine(rect.bottomLeft() + QPoint(offset, -offset), rect.bottomLeft() + QPoint(offset, -8));
        painter.drawLine(rect.bottomLeft() + QPoint(offset, -offset), rect.bottomLeft() + QPoint(8, -offset));

        // Bottom-right corner lines
        painter.drawLine(rect.bottomRight() + QPoint(-offset, -offset), rect.bottomRight() + QPoint(-offset, -8));
        painter.drawLine(rect.bottomRight() + QPoint(-offset, -offset), rect.bottomRight() + QPoint(-8, -offset));

        // --- 新增: 中心绘制一个 "-" 号 ---
        QPoint center = rect.center();
        int lineLength = 12; // -号的长度
        painter.drawLine(center.x() - lineLength / 2, center.y(), center.x() + lineLength / 2, center.y()); // 横线
    }
    
    void drawSystemLogin(QPainter& painter, const QRect& rect, const QColor& color) {
        QPen pen(color, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); // 统一使用3px线条，圆角
        painter.setPen(pen);
        painter.setBrush(Qt::NoBrush); // 确保没有填充

        // 1. 绘制用户轮廓 (纯线条)
        // 头部 (圆形)
        int headRadius = rect.width() / 4;
        QPoint headCenter(rect.center().x(), rect.top() + headRadius + 2); // 稍微向下调整，给人整体感
        painter.drawEllipse(headCenter, headRadius, headRadius);

        // 身体 (U形或弧形)
        int bodyTop = headCenter.y() + headRadius;
        QRectF bodyArcRect(rect.left() + 2, bodyTop, rect.width() - 4, rect.height() - bodyTop + rect.top() - 2);
        // 使用一个大的弧线作为身体，模拟肩膀和下巴
        painter.drawArc(bodyArcRect, 0 * 16, 180 * 16); // 从0度到180度绘制半圆弧
    }

    void drawSettings(QPainter& painter, const QRect& rect, const QColor& color) {
        QPen pen(color, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); // 统一使用3px线条
        painter.setPen(pen);
        painter.setBrush(Qt::NoBrush); // 确保没有填充
        
        QPointF center = rect.center();
        double outerRadius = rect.width() / 2.0 - 2; // 稍微缩小一点，留出边缘
        double innerRadius = outerRadius * 0.7; // 调整内外半径，使齿更明显
        int numTeeth = 8;

        // 绘制齿轮外形轮廓
        QPainterPath path;
        for (int i = 0; i < numTeeth; ++i) {
            double angle1 = i * 2.0 * M_PI / numTeeth;
            double angle2 = (i + 0.5) * 2.0 * M_PI / numTeeth; // 齿尖的角度
            double angle3 = (i + 1) * 2.0 * M_PI / numTeeth;

            // 绘制齿轮基部
            path.moveTo(center.x() + innerRadius * cos(angle1), center.y() + innerRadius * sin(angle1));
            // 绘制齿尖
            path.lineTo(center.x() + outerRadius * cos(angle2), center.y() + outerRadius * sin(angle2));
            // 回到内圆的下一个点
            path.lineTo(center.x() + innerRadius * cos(angle3), center.y() + innerRadius * sin(angle3));
        }
        path.closeSubpath(); // 连接起点和终点
        painter.drawPath(path);

        // 绘制中心圆孔
        painter.drawEllipse(center, innerRadius * 0.4, innerRadius * 0.4); // 中心圆孔
    }
}